<?php
namespace Home\Controller;
use Think\Controller;
class JieController extends CommonController {
	public function Index(){
		//获取getID
		$id = I('get.id');

		$sql = "select t1.*,t2.username,t2.headface from questions t1 inner join user t2  on t1.uid=t2.id where t1.id='{$id}'";
		//取数据
		$question = M()->query($sql);

		$question = current($question);
		// $question = M('questions')->find($id);

		//检查是否已经收藏
		$data['uid']=$_SESSION['id'];
		$data['qid']=$id;
		$res = M('collection')->where($data)->find();
		if ($res) {
			$arr = array('collection'=>1);
			$this->assign('iscollection',$arr);
		}

		//检测用户是否点赞该问题内的回答
		$sql = "select * from goodlike where qid = ".'"'.$id.'"'." and uid = ".'"'.$_SESSION['id'].'"';
		$like = M()->query($sql);
		// var_dump($like);
		$this->assign("like",$like);
		// var_dump(expression)

		//数据传到html页面
		$this->assign("question",$question);

		//添加浏览量
		$this->autoAdd('questions','view_num',"+1",$id);

		//获取热门的10条
		$this->getTopView();

		$this->getTopQuestion();

		// var_dump($question);
		
		$sql = "select t1.*,t2.username,t2.headface from answer t1 inner join user t2 on t1.uid=t2.id where qid = '{$id}'";

		$answers = M()->query($sql);

		// var_dump($answers);
		
		$this->assign('answers',$answers);
		$this->assign('title',$question['title']);
		$this->display();
	}

	public function reply () {
		if ($_SESSION["id"]==null) {
            $arr = array(
				'error'=>0,
				'info'=>'请先登录后回复'
				);
			echo json_encode($arr);
            exit();
        }
		// var_dump(I('post.'));
		$data['content'] = I('post.content');
		$data['uid'] = $_SESSION['id'];
		$data['create_time'] = time();
		$data['qid'] = I('post.qid');
		// var_dump($id);exit;
		$sql = M('answer')->add($data);

		if ($sql) {
			$id = I('post.qid');
			$this->autoAdd('questions','answer_num',"+1",$id);
			$arr = array(
				'error'=>1,
				'info'=>'发布成功'
				);
			echo json_encode($arr);
		}else{
			$arr = array(
				'error'=>0,
				'info'=>'发布失败'
				);
			echo json_encode($arr);
		}


	}

	public function alterjie() {

		if ($_SESSION["id"]==null) {
    		$url = U('home/login/index');
    		header( "Location: $url" ); 
    		exit();
    	}

    	$cates = M('category')->select();
    	// var_dump($cates);

    	$tempArr = [];

    	foreach ($cates as $key => $row) {
    		if ($row['fid']==0) {
    			$tempArr[]=$row;
    		}
    	}

    	foreach ($tempArr as $key => $father) {
    		foreach ($cates as $v) {
    			if ($v['fid']==$father['id']) {
    				$tempArr[$key]['son'][]=$v;
    			}
    		}
    	}


    	//取要修改的问题数据
    	$sql = 'select * from questions where id = '.I('get.id');
    	
    	$questions = current(M()->query($sql));
    	$this->assign('questions',$questions);

    	//var_dump($tempArr);
    	$question = D('Vercode')->getOneVerify();
        $this->assign('title','修改');
    	$this->assign('question',$question);
    	$this->assign('tempArr',$tempArr);
    	$this->display();
	}

	public function alteradd() {
		// var_dump(I('post.'));
		// var_dump(I("get.id"));

		//修改问题数据
		$res = M('questions')->where('id='.I('post.id'))->save(I('post.'));
		if ($res) {
    		$arr = array(
    			'error'=>1,
    			'info'=>"修改成功",
    			'url'=>U('home/jie/index',array('id'=>I('post.id')))
    			);
    		echo json_encode($arr);
		}else{
    		$arr = array(
    			'error'=>0,
    			'info'=>"修改失败"
    			);
    		echo json_encode($arr);
		}

	}

	public function Collection(){
		if ($_SESSION['id']==null) {
			$arr = array(
    			'error'=>3
    			);
    		echo json_encode($arr);
    		exit;
		}
		//收藏功能
		$data['uid']=$_SESSION['id'];
		$data['qid']=I('post.qid');
		
		$res = M('collection')->where($data)->find();
		if (!$res) {
			$data['create_time'] = time();
			$res = M('collection')->add($data);
			if ($res) {
				$arr = array(
    			'error'=>1
    			);
    		echo json_encode($arr);
			}
		
		}else{
			M('collection')->where($data)->delete();
			$arr = array(
    			'error'=>0
    			);
    		echo json_encode($arr);
		}
	}

	public function onlike() {
		//判断是否登陆
		if ($_SESSION['id']==null) {
			$arr = array(
				'error'=>3,
				'info'=>'请先登陆'
				);
			echo json_encode($arr);
			exit();
		}

		//获取点赞uid和aid
		$data['qid'] = I('post.qid');
		$data['uid']=$_SESSION['id'];
		$data['aid'] = I('post.aid');

		$wherelike = array('uid'=>$_SESSION['id'],'aid'=> I('post.aid'));
		$islike = M('goodlike')->where($wherelike)->find();
		
		if ($islike==null) {
			$data['create_time'] = time();
			M('goodlike')->add($data);
			//$sql = "UPDATE answer SET like_num = ".+1." WHERE aid = ";
			// M()->query($sql);
			$this->autoAdd('answer','like_num',"+1",$data['aid']);
			$arr = array('error'=>1,'info'=>'点赞成功');
			echo json_encode($arr);
			exit;
		}else{
			M('goodlike')->where($wherelike)->delete();
			$this->autoAdd('answer','like_num',"-1",$data['aid']);
			$arr = array('error'=>0,'info'=>'取消点赞');
			echo json_encode($arr);
			exit;
		}
		

		
	}






}